package xkp.lesson4;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOCFileCopy {
    public static void main(String[] args) throws IOException {
        copyFile("C:\\Users\\Administrator\\Desktop\\apache-tomcat-9.0.29-windows-x64.zip",
                "Lesson30/file");
    }

    public static void copyFile(String filePath,String destDir) throws IOException {
        File file = new File(filePath);
        FileChannel srcChannel = new FileInputStream(file).getChannel();
        FileChannel descChanel = new FileOutputStream(
                destDir+File.separator+file.getName()).getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int length = srcChannel.read(buffer);
        while(length != -1){
            buffer.flip();
            descChanel.write(buffer);
            buffer.clear();
            length = srcChannel.read(buffer);
        }
        descChanel.force(true);
        descChanel.close();
        srcChannel.close();

    }
}
